﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;

namespace CuadroMandoIntegralFMCH.Form_s
{
  public class SortableBindingList<T> : BindingList<T>
    {
        private bool _isSorted;
        private ListSortDirection _sortDirection;
        private PropertyDescriptor _sortProperty;

        //This override shows the binded object, that our list supports sorting
        protected override bool SupportsSortingCore
        {
            get { return true; }
        }

        //And that it can sort bi-directional
        protected override ListSortDirection SortDirectionCore
        {
            get { return _sortDirection; }
        }

        //And that it can sort by T typed object's properties
        protected override PropertyDescriptor SortPropertyCore
        {

            get { return _sortProperty; }
        }

        //This is the method, what gets called when the sort event occurs in the bound object
        protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
        {
            var items = Items as List<T>;

            if (items != null)
            {
                var pc = new PropertyComparer<T>(prop.Name, direction);
                items.Sort(pc);
                _isSorted = true;
                _sortDirection = direction;
                _sortProperty = prop;
            }
            else
            {
                _isSorted = false;
            }
            OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
        }

        //This shows if our list is already sorted or not
        protected override bool IsSortedCore
        {
            get { return _isSorted; }
        }

        //Removing the sort
        protected override void RemoveSortCore()
        {
            _isSorted = false;
        }

        public SortableBindingList(IList<T> list)
            : base(list)
        {
        }
    }
    public class PropertyComparer<T> : IComparer<T>
    {
        private PropertyInfo _property;
        private ListSortDirection _sortDirection;

        public PropertyComparer(string sortProperty, ListSortDirection sortDirection)
        {
            _property = typeof(T).GetProperty(sortProperty);
            _sortDirection = sortDirection;
        }

        public int Compare(T x, T y)
        {
            var valueX = _property.GetValue(x, null);
            var valueY = _property.GetValue(y, null);

            return _sortDirection == ListSortDirection.Ascending ? Comparer.Default.Compare(valueX, valueY) : Comparer.Default.Compare(valueY, valueX);
        }
    }
}

